//
//  HRGuideViewController.m
//  Hear
//
//  Created by mac on 2017/4/26.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HRGuideViewController.h"
#import "MyPlayTool.h"
#import "HearNavigationController.h"
#import "HRPhoneLoginViewController.h"
#import "UIApplication+YYAdd.h"
#import "HRRecordAudioViewController.h"
#import "HRRecordButton.h"
#import "EMVoiceConverter.h"
@interface HRGuideViewController ()<UIScrollViewDelegate,MyPlayToolDelegate,UIAlertViewDelegate>{
    int angle;
    NSTimer *myTimer;
}
@property (nonatomic, strong)UIImageView *backImageView;
@property(nonatomic, strong)UIScrollView *scrollView;
@property(nonatomic, strong)UIButton *playButton;
@property(nonatomic, strong)UIButton *moreButton;
@property(nonatomic, strong)NSArray *soundArr;
@property(nonatomic, assign)int lastPage;
@property(nonatomic, strong)NSArray *soundTextArr;
@property(nonatomic, strong)UIButton *regestButton;

@end

@implementation HRGuideViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];

    self.soundArr = @[@"木兰1",@"木兰2",@"木兰3",@"木兰4",@"木兰5"];
    self.soundTextArr = @[@"你有多久只是和好朋友们在朋友圈里面点赞互动，而没有听到ta们的声音了？你想念ta们吗？",@"坐飞机或者火车时，旁边坐着和自己年龄相仿的陌生人，如果ta对你报以微笑，你会以微笑回应吗？你会主动和ta攀谈吗？",@"你是不是很久没有和你的合租室友交谈过了？如果你们有机会聊聊天，你会想和ta说些什么呢？",@"生活中遇到开心或者不开心的事，你会憋在心里还是说出来呢？你能感受到大家对你的关心和爱护吗？",@"假如你发现一个陌生人与曾经的你有相似的人生困惑，你会愿意主动去帮助ta吗？"];
    
    [self setupView];
    
    
    if (@available(iOS 11.0, *))
    {
        self.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    else
    {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    self.lastPage = 0;
    

    
}



- (void)setupView{
    self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:self.scrollView];
    self.scrollView.delegate = self;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.contentSize = CGSizeMake(kFullWidth, kFullHeight*5);
    
    
    for (int i =0; i<5; i ++) {
        UIImageView *backImageView = [UIImageView new];
        backImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"bg%d",i+1]];
        backImageView.contentMode = UIViewContentModeScaleAspectFill;
        [self.scrollView addSubview:backImageView];
        backImageView.frame = CGRectMake(0, kFullHeight*i, kFullWidth, kFullHeight);
    }
    
    
    self.playButton = [UIButton buttonWithType:0];
    [self.playButton setImage:[UIImage imageNamed:@"引导_语音"] forState:0];
    [self.playButton addTarget:self action:@selector(playButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.playButton];
    self.playButton.frame = CGRectMake(kFullWidth-50, 30, 30, 30);
    
    myTimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(stransAnimation) userInfo:nil repeats:YES];

    
    self.moreButton = [UIButton buttonWithType:0];
    [self.moreButton setImage:[UIImage imageNamed:@"引导_箭头"] forState:0];
    [self.view addSubview:self.moreButton];
    self.moreButton.frame = CGRectMake((kFullWidth-30)/2, kFullHeight-50, 30, 30);
    [self startMoreAnimation];
    
    

    self.regestButton = [[UIButton alloc] initWithFrame:CGRectMake(kFullWidth-70, self.moreButton.top+20, 50, 20)];
    [self.regestButton setTitle:@"去注册" forState:0];
    [self.regestButton setTitleColor:[UIColor whiteColor] forState:0];
    self.regestButton.titleLabel.font = CustomUIFont(14);
    [self.view addSubview:self.regestButton];
    self.regestButton.layer.cornerRadius = 5;
    self.regestButton.layer.borderColor = [UIColor whiteColor].CGColor;
    self.regestButton.layer.borderWidth = 1;
    self.regestButton.hidden = YES;
    [self.regestButton addTarget:self action:@selector(regestButtonClick) forControlEvents:UIControlEventTouchUpInside];

}






- (void)playButtonClick{
    self.playButton.selected = !self.playButton.isSelected;
    
    if (self.playButton.selected){
        [myTimer invalidate];
        myTimer = nil;
        [[MyPlayTool sharePlayTool] pause];
    }else{
        myTimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(stransAnimation) userInfo:nil repeats:YES];
        [[MyPlayTool sharePlayTool] play];
    }
}

-(void)myPlayToolStopDelegate{
    [myTimer invalidate];
    myTimer = nil;
    self.playButton.selected = YES;
}


-(void)stransAnimation{
  self.playButton.transform = CGAffineTransformMakeRotation(angle * (M_PI / 180.0f));
    angle += 10;
}

- (void)startMoreAnimation{
    WeakSelf
    [UIView animateWithDuration:1.0 animations:^{
        weakSelf.moreButton.top = kFullHeight-65;
        weakSelf.moreButton.alpha = 0;
    } completion:^(BOOL finished) {
        weakSelf.moreButton.top = kFullHeight-50;
        weakSelf.moreButton.alpha = 1;
        [weakSelf startMoreAnimation];
    }];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    WeakSelf
    [UIView animateWithDuration:1.0 animations:^{
        weakSelf.playButton.alpha = 1;
    }];
    
    int  curentPage = scrollView.contentOffset.y/kScreenHeight;
    if (self.lastPage != curentPage){
        [[MyPlayTool sharePlayTool] stop];
        [[MyPlayTool sharePlayTool] loadLocalMusicUsePath:[[NSBundle mainBundle] pathForResource:self.soundArr[curentPage] ofType:@"mp3"]];
        if (self.playButton.selected){
            [[MyPlayTool sharePlayTool] pause];
        }
        self.lastPage = curentPage;
    }
    

}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    DDLog(@"==============%f",scrollView.contentOffset.y);
    WeakSelf
    [UIView animateWithDuration:1.0 animations:^{
        weakSelf.playButton.alpha = 0  ;
    }];
    DDLog(@"%f",kScreenHeight*self.soundArr.count);
    if (scrollView.contentOffset.y >=(kScreenHeight*(self.soundArr.count-1))){
        self.regestButton.hidden = NO;
    }else{
        self.regestButton.hidden = YES;
    }
}

- (void)regestButtonClick{
    [[MyPlayTool sharePlayTool] stop];
    
    [self.navigationController removeFromParentViewController];
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window removeAllSubviews];
    window = nil;
    HearNavigationController *nav = [[HearNavigationController alloc] initWithRootViewController:[HRPhoneLoginViewController new]];
    
    [UIApplication sharedApplication].keyWindow.rootViewController = nav;
}



-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
    [[MyPlayTool sharePlayTool] loadLocalMusicUsePath:[[NSBundle mainBundle] pathForResource:self.soundArr[0] ofType:@"mp3"]];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
